package com.cplatform.movie.back.sale.web;

import java.util.Date;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.cplatform.movie.back.sale.service.ImageDetailService;
import com.cplatform.movie.back.service.BsFileService;
import com.cplatform.movie.back.utils.AppConfig;
import com.cplatform.movie.back.utils.JsonRespWrapper;
import com.cplatform.util2.FileTools;

@Controller
@RequestMapping("/imgUpload")
public class ImageController {
	Logger log = Logger.getLogger(ImageController.class);
	@Autowired
	private BsFileService bsFileService;
	@Autowired
	private AppConfig appConfig;
	@Autowired
	private ImageDetailService imageDetailService;
	
	/**
	 * @Title: 商品封面图上传 
	 * <br>
	 * @param id
	 * @param Filedata
	 * @param model
	 * @return    
	 * @throws
	 */
	@RequestMapping(value = "/cover/{id}")
	@ResponseBody
	public Object imgAdd(@PathVariable Long id, MultipartFile Filedata, Model model) {
		String fileName = Filedata.getOriginalFilename();
		String filePath = null;
		try {
			String ext = FileTools.getExtFilename(fileName);
			filePath = bsFileService.uploadBsImgFile(Filedata.getInputStream(), ext, BsFileService.ITEM_SALE_COVER_KEY, id, fileName);
		}
		catch (Exception e) {
			log.error("商品图片上传失败：" + e.getMessage());
		}
		return filePath;
	}
	
	/**
	 * 上传商品详情图片
	 * 
	 * @param id
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/info/{id}")
	@ResponseBody
	public Object batchImgAdd(@PathVariable Long id, MultipartFile Filedata, Model model) {
		String fileName = Filedata.getOriginalFilename();
		String filePath = null;
		try {
			String ext = FileTools.getExtFilename(fileName);
			filePath = bsFileService.uploadBsImgFile(Filedata.getInputStream(), ext, BsFileService.ITEM_SALE_INFO_KEY, new Date().getTime(),fileName);
		}
		catch (Exception e) {
			log.error("商品详情图片上传失败：" + e.getMessage());
		}
		return filePath;
	}
	
	/**
	 * 删除商品详情图片
	 * 
	 * @param id
	 * @param model
	 * @param request
	 * @return
	 */

	@RequestMapping(value = "/info/deleteItemImgAjax/{id}")
	@ResponseBody
	public Object imgDel(@PathVariable Long id, Model model, HttpServletRequest request) {
		try {
			String imgPath = request.getParameter("imgPath");
			bsFileService.rmFile(appConfig.getUploadItemInfoDir() + imgPath);
			for (int i = 0; i < 3; i++) {
				bsFileService.rmFile(appConfig.getUploadItemInfoDir() + imgPath.replaceAll("N", "N" + i));
			}
			if (id > 0) {
				imageDetailService.deleteById(id);
			}
			return JsonRespWrapper.successAlert("删除成功！");
		}
		catch (Exception e) {
			log.error("删除商品详情图片失败：" + e.getMessage());
			return JsonRespWrapper.successAlert("删除失败！");
		}
	}
	
	/**
	 * @Title: 商品封面图上传 
	 * <br>
	 * @param id
	 * @param Filedata
	 * @param model
	 * @return    
	 * @throws
	 */
	@RequestMapping(value = "/type/{id}")
	@ResponseBody
	public Object typeImgAdd(@PathVariable Long id, MultipartFile Filedata, Model model) {
		String fileName = Filedata.getOriginalFilename();
		String filePath = null;
		try {
			String ext = FileTools.getExtFilename(fileName);
			filePath = appConfig.getUploadItemTypePath() + bsFileService.uploadBsImgFile(Filedata.getInputStream(), ext, BsFileService.ITEM_SALE_TYPE_KEY, id, fileName);
		}
		catch (Exception e) {
			log.error("分类图片上传失败：" + e.getMessage());
		}
		return filePath;
	}
	
	/**
	 * @Title: 商户图片上传 
	 * <br>
	 * @param id
	 * @param Filedata
	 * @param model
	 * @return    
	 * @throws
	 */
	@RequestMapping(value = "/store/{id}")
	@ResponseBody
	public Object storeImgAdd(@PathVariable Long id, MultipartFile Filedata, Model model) {
		String fileName = Filedata.getOriginalFilename();
		String filePath = null;
		try {
			String ext = FileTools.getExtFilename(fileName);
			filePath = appConfig.getUploadStoreInfoPath() + bsFileService.uploadBsImgFile(Filedata.getInputStream(), ext, BsFileService.STORE_INFO_KEY, id, fileName);
		}
		catch (Exception e) {
			log.error("商户图片上传失败：" + e.getMessage());
		}
		return filePath;
	}
	
	/**
	 * @Title: 活动图片上传 
	 * <br>
	 * @param id
	 * @param Filedata
	 * @param model
	 * @return    
	 * @throws
	 */
	@RequestMapping(value = "/activities/{id}")
	@ResponseBody
	public Object activitiesImgAdd(@PathVariable Long id, MultipartFile Filedata, Model model) {
		String fileName = Filedata.getOriginalFilename();
		String filePath = null;
		try {
			String ext = FileTools.getExtFilename(fileName);
			filePath = appConfig.getUploadActivitiesInfoPath() + bsFileService.uploadBsImgFile(Filedata.getInputStream(), ext, BsFileService.ACTIVITIES_INFO_KEY, id, fileName);
		}
		catch (Exception e) {
			log.error("活动图片上传失败：" + e.getMessage());
		}
		return filePath;
	}
	
}
